package com.eckey.lab.exception;

import com.eckey.lab.enums.ExceptionEnum;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

/**
 * @Author: Marinc
 * @CreateTime: 2024-06-17  14:16
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class BaseException extends RuntimeException {

    private Object[] args;

    private ExceptionEnum exceptionEnum;

    public BaseException() {
    }

    public BaseException(String message, Throwable t) {
        super(message, t);
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public ExceptionEnum getErrorCode() {
        return exceptionEnum;
    }

    public void setErrorCode(ExceptionEnum errorCode) {
        this.exceptionEnum = errorCode;
    }

    public BaseException(ExceptionEnum errorCode, Object... args) {
        this.exceptionEnum = errorCode;
        this.args = args;
    }

    public BaseException(ExceptionEnum errorCode, Throwable t, Object... args) {
        super(t);
        this.exceptionEnum = errorCode;
        this.args = args;
    }

    @Override
    public String getMessage() {
        String message;
        if (exceptionEnum != null) {
            message = MessageFormat.format(exceptionEnum.getErrorMsg(), args);
        } else {
            message = super.getMessage();
        }
        return message;
    }
}